package cn.onlineclass.common;

import cn.onlineclass.entity.OMajor;
import cn.onlineclass.service.MajorService;
import cn.onlineclass.util.StringUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MajorController {
    //模糊查询
    public void majorLimit(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"),1);
        AjaxPage search = MajorService.MAJOR.search(pageNo,10);
        String str = JSON.toJSONStringWithDateFormat(search,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(str);
    }

    //增加
    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String name =request.getParameter("name");
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
       // Date createtime =StringUtil.parse(request.getParameter("createtime"));
       // Date modifytime =StringUtil.parse(request.getParameter("modifytime"));
        String description =request.getParameter("description");
        int ratationid =StringUtil.toInt(request.getParameter("ratationid"));
        OMajor major =new OMajor(name,inuse,null,null,description,ratationid);
        AjaxResult add = MajorService.MAJOR.add(major);
       return add;
    }

    //接受修改的id
    public void get(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        MajorService major =new MajorService();
        AjaxResult get = major.get(id);
        String str =JSON.toJSONString(get);
        response.getWriter().print(str);
    }

    //修改
    public AjaxResult update (HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        String name = request.getParameter("name");
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
        String description = request.getParameter("description");
        int ratationid = StringUtil.toInt(request.getParameter("ratationid"));
        OMajor major =new OMajor(id,name,inuse,null,null,description,ratationid);
        AjaxResult update = MajorService.MAJOR.update(major);
        return update;
    }


    //删除
    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        OMajor major =new OMajor();
        major.setId(id);
        major.setInuse(false);
        AjaxResult del = MajorService.MAJOR.del(id);
        return del;
    }
}
